/*
 *  您可以更改此项目但请不要删除作者署名谢谢，否则根据中华人民共和国版权法进行处理.
 *  You may change this item but please do not remove the author's signature,
 *  otherwise it will be dealt with according to the Copyright Law of the People's Republic of China.
 *
 *  如果需要联系到我请加Wechat: yangbuyiya 备注交流学习
 *  If you need to contact me, please add Wechat: yangbuyiya as a note to exchange and learn.
 *  yangbuyi Copyright (c) https://yby6.com 2024.
 */

package com.yby6.config.mybatisPlus;

import cn.hutool.core.net.NetUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.solon.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.solon.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.solon.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor;
import com.yby6.config.mybatisPlus.ext.MyLogicSqlInjector;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

import javax.sql.DataSource;

@Configuration
public class MybatisPlusConfig {
    @Bean(value = "db1", typed = true)
    public DataSource db1(@Inject("${plus.db1}") HikariDataSource ds) {
        return ds;
    }

    @Bean
    public void db1_cfg(@Db("db1") MybatisConfiguration cfg,
                        @Db("db1") GlobalConfig globalConfig) {
        MybatisPlusInterceptor plusInterceptor = new MybatisPlusInterceptor();
        plusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 分页插件
        plusInterceptor.addInnerInterceptor(paginationInnerInterceptor());
        // 乐观锁插件
        plusInterceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        // 针对 update 和 delete 语句 作用: 阻止恶意的全表更新删除
        plusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        
        cfg.setCacheEnabled(false);
        cfg.addInterceptor(plusInterceptor);
        
        globalConfig.setSqlInjector(new MyLogicSqlInjector());
        // 使用网卡信息绑定雪花生成器 防止集群雪花ID重复
        globalConfig.setIdentifierGenerator(new DefaultIdentifierGenerator(NetUtil.getLocalhost()));
    }
    
    /**
     * 分页插件，自动识别数据库类型
     */
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        // 分页合理化
        paginationInnerInterceptor.setOverflow(true);
        return paginationInnerInterceptor;
    }
    
    /**
     * 乐观锁插件
     */
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
        return new OptimisticLockerInnerInterceptor();
    }

    
    @Bean
    public MybatisSqlSessionFactoryBuilder factoryBuilderNew(){
        return new MybatisSqlSessionFactoryBuilderImpl();
    }
}
